package com.epfresh.api.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-4-10
 * @Description
 */

public class DateUtil {

    // -3,time1,time2都为空，1,time2大，-1,time1大，0,一样大 ，-2,异常
    public static int compare(String time1, String time2) {

        if ((time1 == null || "".equals(time1)) && time2 != null && !"".equals(time2)) {
            Log.e("c","----1");
            return 1;
        }
        if ((time2 == null || "".equals(time2)) && time1 != null && !"".equals(time1)) {
            Log.e("c","-----1");
            return -1;
        }
        if ((time2 == null || "".equals(time2)) && (time1 == null || "".equals(time1))) {
            Log.e("c","-----3");
            return -3;
        }
        try {
            //如果想比较日期则写成"yyyy-MM-dd"就可以了
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            //将字符串形式的时间转化为Date类型的时间
            Date a = null;

            a = sdf.parse(time1);

            Date b = sdf.parse(time2);
            //Date类的一个方法，如果a早于b返回true，否则返回false
            if (a.before(b))//b大
            {
                return 1;

            } else if (b.before(a)) {
                return -1;
            } else
                return 0;
        /*
         * 如果你不喜欢用上面这个太流氓的方法，也可以根据将Date转换成毫秒
        if(a.getTime()-b.getTime()<0)
            return true;
        else
            return false;
        */

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -2;
    }

    public static String getPromotionDate(String startDate,String endDate){
        try{
            SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");//24小时制
            Date s = sdformat.parse(startDate);
            Date e = sdformat.parse(endDate);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//24小时制

            String sd = sdf.format(s);
            String ed = sdf.format(e);

            return sd+"-"+ed;

        }catch (Exception e){

            return "";
        }
    }


    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }


    public static String getTodayStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");



        return sdf.format(new Date());
    }


    public static long formatTimeLong(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getTimeLongToString(long time){

        long h = time / (1000 * 3600);
        long m = (time-h*3600*1000) / (1000 * 60);
        long s = (time-h*3600*1000 - m*1000*60) / 1000;

        return String.format("%02d:%02d:%02d",(int)h,(int)m,(int)s);

    }
    public static String[] getTimeLongToStrings(long timeMills){

        String[] s = null;
        String time = getTimeLongToString(timeMills);
        if (time != null && time.length() == 8){
            //Log.e("cdt.getCountDownTime()","time2:"+time);
            String hourH = time.substring(0, 1);
            String hourL = time.substring(1, 2);

            String minutesH = time.substring(3, 4);
            String minutesL = time.substring(4, 5);

            String secondH = time.substring(6, 7);
            String secondL = time.substring(7, 8);

           s = new String[]{hourH,hourL,minutesH,minutesL,secondH,secondL};


        }
        return s;

    }

}
